package com.hhu.molibs;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.webrtc.AudioSource;
import org.webrtc.AudioTrack;
import org.webrtc.EglBase;
import org.webrtc.PeerConnection;
import org.webrtc.PeerConnectionFactory;
import org.webrtc.SurfaceTextureHelper;
import org.webrtc.SurfaceViewRenderer;
import org.webrtc.VideoCapturer;
import org.webrtc.VideoFrame;
import org.webrtc.VideoSink;
import org.webrtc.VideoSource;
import org.webrtc.VideoTrack;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.UUID;


/*
* 实现基于webRtc和MQTT的音视频通话
* */
public class THhuRtc extends Handler {
    public final static String TAG = "THhuRtc";

    public final static  int EventCode_Initing = 1000;
    public final static  int EventCode_Inited= 1010;
    public final static  int EventCode_InitFactory= 1020;


    Context mCtx;
    Map<String , THhuPeerConnection> mPcs = new HashMap<String, THhuPeerConnection>();
    THhuRtcConfig mConfig = new THhuRtcConfig();
    MqttClient mMqClient;
    PeerConnectionFactory mPeerConnectionFactory;
    EglBase mRootEglBase;
    SurfaceTextureHelper mSurfaceTextureHelper;
    VideoCapturer mVideoCapturer;
    VideoSource mVideoSource;
    VideoTrack mVideoTrack;
    AudioSource mAudioSource;
    AudioTrack mAudioTrack;
    LinkedList<PeerConnection.IceServer> mIceServers;

    public  void init(){
        try{
            this.sendEmptyMessage(EventCode_Initing);
            initFactory();
            initVideoAudio();
            initIceServer();
            openMqClient();
            this.sendEmptyMessage(EventCode_Inited);
        }
        catch (Exception er){
            Log.e(TAG, "init: ", er );
        }
    }
    public  void deInit(){
        try{

        }
        catch (Exception er){
            Log.e(TAG, "deInit: ", er );
        }
    }
    public  void startCapture(SurfaceViewRenderer previewRender){
        try{

        }
        catch (Exception er){
            Log.e(TAG, "startCapture: ", er );
        }
    }
    public  void stopCapture(){
        try{

        }
        catch (Exception er){
            Log.e(TAG, "stopCapture: ", er );
        }
    }
    public  THhuPeerConnection openPc(){
        THhuPeerConnection pc = new THhuPeerConnection();
        try{

        }
        catch (Exception er){
            Log.e(TAG, "createPc: ", er );
        }
        return  pc;
    }
    public  THhuPeerConnection getPc(String pcId){
        THhuPeerConnection pc = null;
        try{

        }
        catch (Exception er){
            Log.e(TAG, "getPc: ", er );
        }
        return  pc;
    }
    public  void closePc(){
        try{

        }
        catch (Exception er){
            Log.e(TAG, "closePc: ", er );
        }
    }
    public  Map<String, THhuPeerConnection> getPcs( ){
        return  mPcs;
    }


    public  THhuRtcConfig getConfig(){
        return mConfig;
    }
    public  MqttClient getMqClient (){
        return mMqClient;
    }

    void initFactory(){
        try{
            this.sendEmptyMessage(EventCode_InitFactory);

        }
        catch (Exception er){
            Log.e(TAG, "initFactory: ", er );
        }
    }
    void initVideoAudio(){
        try{

        }
        catch (Exception er){
            Log.e(TAG, "initVideoAudio: ", er );
        }
    }
    void initIceServer(){
        try{

        }
        catch (Exception er){
            Log.e(TAG, "initIceServer: ", er );
        }
    }
    void openMqClient(){
        try{

        }
        catch (Exception er){
            Log.e(TAG, "openMqClient: ", er );
        }
    }

    // config
    public static class THhuRtcConfig{
        public String rtcId = UUID.randomUUID().toString().replace("-","").toLowerCase().substring(0,15);
        public String rtcCode = "";
        public String appCode = "hhuRtcApp";
        public String groupCode = "";
        public Map<String , String> tag = new HashMap<String, String>();

        public String mqUrl="";
        public String mqUid="";
        public String mqPwd="";

        public String stunUrl="";
        public String turnUrl ="";
        public String turnUid ="";
        public String turnPwd ="";

    }
    public static class THhuPeerConnection{
        THhuRtc rtc ;
        SurfaceViewRenderer remoteView;
        public String pcId ="pc_"+
                UUID.randomUUID().toString().replace("-","").toLowerCase().substring(0,20);
        public String pcCode ="";
        public Map<String ,String> tag = new HashMap<String, String>();
    }
    public static class THhuVideoSink implements org.webrtc.VideoSink {
        private org.webrtc.VideoSink mTarget;
        @Override
        synchronized public void onFrame(VideoFrame frame) {
            if (mTarget == null) {
                Log.d(TAG, "Dropping frame in proxy because target is null.");
                return;
            }
            mTarget.onFrame(frame);
        }
        synchronized void setTarget(org.webrtc.VideoSink target) {
            this.mTarget = target;
        }
    }
}
